package com.hl.security.config;

import com.hl.security.UserAuthenticationFailureHandler;
import com.hl.security.UserAuthenticationSuccessHandler;
import com.hl.security.UserLogoutSuccessHandler;
import com.hl.security.UserServicesImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.authentication.encoding.BasePasswordEncoder;
import org.springframework.security.authentication.encoding.ShaPasswordEncoder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

/**
 * Created by aaron on 2017/1/17.
 */
public abstract class  WebHlSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                .antMatchers("/", "/home").permitAll()
                .anyRequest().authenticated()
                .and()
                .formLogin().loginPage("/system/login.do").loginProcessingUrl("/login_c").failureHandler(getSecurityConfigInfo().getUserAuthenticationFailureHandler())
                .successHandler(getSecurityConfigInfo().getUserAuthenticationSuccessHandler())
                .permitAll()
                .and()
                .logout().logoutUrl("/logout").logoutSuccessHandler(getSecurityConfigInfo().getUserLogoutSuccessHandler())
                .permitAll();
    }


    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(getSecurityConfigInfo().getUserDetailsService()).passwordEncoder(getSecurityConfigInfo().getPasswordEncoder());
    }

    public abstract SecurityConfigInfo getSecurityConfigInfo();

}
